/**
 * BBD Service Inc
 * All Rights Reserved @2018
 */
package com.magicmovie.web.controller;

import com.magicmovie.entity.Banner;
import com.magicmovie.entity.Dictionary;
import com.magicmovie.entity.IndexCategory;
import com.magicmovie.utils.AjaxMsg;
import com.magicmovie.utils.ValidUtil;
import com.magicmovie.web.service.BannerService;
import com.magicmovie.web.service.DictionaryService;
import com.magicmovie.web.service.MovieService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 輪播管理
 *
 * @author
 */
@Controller
@RequestMapping("/banner")
public class BannerController {
    @Resource
    private BannerService bannerService;
    @Resource
    private MovieService movieService;
    @Resource
    private DictionaryService dictionaryService;

    @RequestMapping(value = "/list")
    public String list(Model model) {
        Map map = new HashMap<>();
        List list = bannerService.queryList(map);
        model.addAttribute("list", list);
        return "banner/list";
    }

    @RequestMapping(value = "/input")
    public String input(Model model, Integer id) {
        if (id != null && id != 0) {
            Banner banner = bannerService.queryObject(id);
            model.addAttribute("entity", banner);
        } else {
            model.addAttribute("entity", new Banner());
        }

        Map map = new HashMap<>();

        List<Map<String, Object>> movies = movieService.queryList(map);
        model.addAttribute("movies", movies);

        List<Dictionary> dictionaries = dictionaryService.queryByParams(map);
        model.addAttribute("dictionaries", dictionaries);

        return "banner/input";
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public AjaxMsg save(Banner banner) {
        if (banner.getId() == null || banner.getId() == 0) {
            bannerService.save(banner);
        } else {
            bannerService.update(banner);
        }
        return new AjaxMsg();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public AjaxMsg delete(Integer id) {
        bannerService.delete(id);
        return new AjaxMsg();
    }

}
